
const tokens = {
  admin: {
    password: 123456,
    token: 'admin-token'
  },
  editor: {
    password: 123456,
    token: 'editor-token'
  }
}

// get请求从config.url获取参数，post从config.body中获取参数
function paramObj(url: string): object {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"') +
    '"}'
  )
}

const api = [
  // 登陆接口
  {
    url: '/user/login',
    type: 'post',
    response: (config: any) => {
      // console.log(tokens)
      // console.log(config)
      //如果get是url传参:path或params就是config.url截取获取参数，get,post使用data传参就都在config.body获取参数，json先转成JSON.parse()
      const { username } = JSON.parse(config.body)
      const token = tokens[username as keyof typeof tokens]?tokens[username as keyof typeof tokens].token:""
      const password=tokens[username as keyof typeof tokens]?tokens[username as keyof typeof tokens].password:""
      if (token && password) {
        return {
          code: 0,
          message: '操作成功',
          data: {
            token: token
          }
        }
      }
      return {
        code: 1,
        message: '用户信息错误，请重试~',
        data: {}
      }
    }
  }
]
export default api